//
//  ZYPopLayerRule.m
//  DesignMode
//
//  Created by wangzhipan on 2024/11/18.
//

#import "ZYPopLayerRule.h"

@interface ZYPopLayerRule()
@property (nonatomic, copy, readwrite) NSString *popLayerId;
@end

@implementation ZYPopLayerRule

+ (BOOL)isExistRule:(NSString *)popLayerId
{
    //根据后台返回的引导配置，查一下是否存在这个poplayerId的配置
    return YES;
}

- (instancetype)initWithPopLayerId:(NSString *)popLayerId
{
    if (self = [super init]) {
        self.popLayerId = popLayerId;
        
        [self readConfig];
    }
    return self;
}

//根据ID读取从服务器下载回来的引导规范配置，
- (void)readConfig
{
    //测试数据
    self.showTimes = 3;
    self.duration = 10;
    self.isToQueue = YES;
    self.isFourceShow = false;
    self.enableResume = false;
}

- (BOOL)enableShow
{
    //读取本地关于这个id的引导层是否还能展示，可以继续封装一下，这里简单处理
    NSString *key = [NSString stringWithFormat:@"popLayerRule_showTimes_%@", self.popLayerId];
    NSUInteger didShowTimes = [[[NSUserDefaults standardUserDefaults] objectForKey:key] integerValue];
    if (didShowTimes >= self.showTimes) {
        //日志输出下，什么原因不给展示
        NSLog(@"ZYPopLayer, layerId: %@ enableShow false: didShowTimes == %lu, showTimes == %lu, Exceedance limit", self.popLayerId, didShowTimes, self.showTimes);
        return false;
    }
    //可以追加规则，比如命中新用户等，也不展示气泡
    
    return true;
}



- (void)didShow
{
    NSString *key = [NSString stringWithFormat:@"popLayerRule_showTimes_%@", self.popLayerId];
    NSUInteger didShowTimes = [[[NSUserDefaults standardUserDefaults] objectForKey:key] integerValue];
    didShowTimes++;
    [[NSUserDefaults standardUserDefaults] setObject:@(didShowTimes) forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)isEqual:(ZYPopLayerRule *)object
{
    if (self == object || [self.popLayerId isEqualToString:object.popLayerId]) {
        return YES;
    }
    return NO;
}

@end
